package com.situ.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/*")
//专门处理登录的过滤器
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //session　会话
        HttpSession session = req.getSession();
        String uri = req.getRequestURI();//当前请求的请求地址
        Object o = session.getAttribute("current");
        if (o != null || uri.equals("/login") || uri.startsWith("/css/") || uri.startsWith("/img/") || uri.startsWith("/js/") || uri.startsWith("/plugins/")) {
            //进入servlet之前
            chain.doFilter(request, response);
            //从servlet里出来之后执行
        } else {
            resp.sendRedirect("/login");
        }
    }
}
